package com.spark.actions.fin.gld;

import java.util.Map;
import java.util.regex.Pattern;

import com.spark.actions.sys.DefaultAction;
import com.spark.core.Message;
import com.spark.core.SparkContext;

@SuppressWarnings("unchecked")
public class CurrencyAction extends DefaultAction {
	
	public static final String FIELD_CURRENCY_CODE = "CURRENCY_CODE";
	public static final String FIELD_CURRENCY_NAME = "CURRENCY_NAME";
	public static final String FIELD_CURRENCY_PRECISION = "CURRENCY_PRECISION";
	public static final String FIELD_CONVERSION_RATE = "CONVERSION_RATE";
	
	//TODO:从当前系统帐套中获取(缓存)
	public static final String DEFAULT_STANDARD_MONEY = "CNY";
	
	private static final Pattern PATTERN_CURRENCY_CODE = Pattern.compile("^[A-Z]{3}$");
	private static final Pattern PATTERN_CURRENCY_PRECISION = Pattern.compile("^\\d+$");
	private static final Pattern PATTERN_CONVERSION_RATE = Pattern.compile("^\\d+(\\.\\d+)?$");	
	
	private static final String VALID_CURRENCY_CODE = "币种编码只能为三位的英文大写字母!";
	private static final String VALID_CURRENCY_NAME = "币种名称不能为空!";
	private static final String VALID_CURRENCY_PRECISION = "精度必须大于零!";
	private static final String VALID_CONVERSION_RATE = "汇率必须大于零!";
	
	
	private static final String SQL_SELECT_STATEMENT = "gld_currency.select";
	private static final String SQL_CREATE_STATEMENT = "gld_currency.insert";
	private static final String SQL_UPDATE_STATEMENT = "gld_currency.update";
	private static final String SQL_DELETE_STATEMENT = "gld_currency.delete";
	
	
	/**
	 * 查询币种
	 * 
	 * @param context
	 * @throws Exception
	 */
	public void query(SparkContext context) throws Exception {
		doQuery(context, SQL_SELECT_STATEMENT);
	}
	
	/**
	 * 创建币种.
	 * @param context
	 * @throws Exception
	 * TODO:发送新建消息
	 */
	
	public void create(SparkContext context) throws Exception {
		doExecute(context, SQL_CREATE_STATEMENT, ACTION_INSERT);
	}
	
	/**
	 * 更新币种.
	 * @param context
	 * @throws Exception
	 * TODO:发送更新消息
	 */
	public void update(SparkContext context) throws Exception {
		doExecute(context, SQL_UPDATE_STATEMENT, ACTION_UPDATE);
	}
	
	/**
	 * 删除币种.
	 * 
	 * @param context
	 * @throws Exception
	 * TODO:判断当前币种是否已经被使用? 还是改成无法删除，只能失效？
	 * TODO:发送删除消息
	 */
	public void delete(SparkContext context) throws Exception {
		doExecute(context, SQL_DELETE_STATEMENT, ACTION_DELETE);
	}
	
	
	
	protected boolean validateInset(Map map, Message message){
		return isNotNullAndRestrict(map.get(FIELD_CURRENCY_CODE),PATTERN_CURRENCY_CODE, message,VALID_CURRENCY_CODE)&&
			   isNotNull(map.get(FIELD_CURRENCY_NAME), message,VALID_CURRENCY_NAME)&&
		   	   isNotNullAndRestrict(map.get(FIELD_CURRENCY_PRECISION),PATTERN_CURRENCY_PRECISION, message, VALID_CURRENCY_PRECISION)&&
		   	   isNotNullAndRestrict(map.get(FIELD_CONVERSION_RATE),PATTERN_CONVERSION_RATE, message,VALID_CONVERSION_RATE);
	}
	
	protected boolean validateUpdate(Map map, Message message){
		return isNotEmpty(map.get(FIELD_CURRENCY_NAME), message,VALID_CURRENCY_NAME) && 
			   isNullOrRestrict(map.get(FIELD_CURRENCY_PRECISION),PATTERN_CURRENCY_PRECISION,  message,VALID_CURRENCY_PRECISION) && 
			   isNullOrRestrict(map.get(FIELD_CONVERSION_RATE),PATTERN_CONVERSION_RATE, message,VALID_CONVERSION_RATE);
	}
	
	protected void writeConflictingMessage(Map map ,Message message){
		message.setWarning("存在相同编码的币种!");
		message.setWidth(300);
		message.setHeiht(150);
	}
	
}
